using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 对外接口类
    /// </summary>
    public class SceneBuilder
    {
        /// <summary>
        /// 场景名
        /// </summary>
        private string _sceneName;

        /// <summary>
        /// 场景碰撞文件名
        /// </summary>
        // private string _colliderName;
        /// <summary>
        /// 主角的Transform
        /// </summary>
        private Transform _detector;

        private int _progress;

        /// <summary>
        /// 地图加载进度
        /// </summary>
        public int Progress
        {
            get { return _progress; }
            private set => _progress = value;
        }

        private bool _isUnLoaded;

        private BigWorld _bigWorld;

        public SceneBuilder(string sceneName, Transform detector)
        {
            _sceneName = sceneName;
            _detector = detector;
            Load();
        }

        /// <summary>
        /// 场景加载
        /// </summary>
        public void Load()
        {
            // 发一个LoadScene的消息
            Progress = 10;
            string sceneFilePath = $"Scene/Data/SceneData_{_sceneName}";
            // 用加载器加载地图二进制数据
            // if (_sceneDataLoader == null)
            // {
            //     _sceneDataLoader = RequestHandler.Get(sceneFilePath, OnSceneDataLoad);
            // }
            OnSceneDataLoad(Resources.Load<TextAsset>(sceneFilePath));
        }

        /// <summary>
        /// 场景卸载
        /// </summary>
        public void UnLoad()
        {
            if (_isUnLoaded) return;
            _isUnLoaded = true;
            if (_bigWorld != null)
            {
                _bigWorld.Clear();
                Object.DestroyImmediate(_bigWorld.gameObject);
                _bigWorld = null;
            }

            Debug.Log($"UnLoad Scene {_sceneName}");
        }

        /// <summary>
        /// 关联玩家主角
        /// </summary>
        public void SetPlayer(Transform detector)
        {
            _detector = detector;
            if (_bigWorld != null)
            {
                _bigWorld.SetPlayer(detector);
            }
        }

        /// <summary>
        /// 地图二进制数据加载完的回调
        /// </summary>
        public void OnSceneDataLoad(TextAsset asset)
        {
            if (asset != null)
            {
                SceneData data = new SceneData();
                data.Deserialize(asset.bytes);
                GameObject go = new GameObject(_sceneName);
                _bigWorld = go.AddComponent<BigWorld>();
                // string colliderPath = _colliderName;
                // if (string.IsNullOrEmpty(_colliderName))
                // {
                //     colliderPath = string.Empty;
                // }
                // else
                // {
                //     colliderPath = $"Scene/Collider/{_sceneName}_collider.prefab";
                // }
                _bigWorld.Load(data);
                _bigWorld.SetPlayer(_detector);
            }
        }
    }
}